<template>
  <div class="app-container">
    <el-form ref="dataForm" :rules="rules" :model="catalogueForm" label-position="left" label-width="140px">
      <el-form-item label="标题" prop="title">
        <el-input v-model="catalogueForm.title" />
      </el-form-item>
      <el-form-item label="图片">
        <Upload v-model="catalogueForm.avatar" />
      </el-form-item>
      <el-form-item label="介绍">
        <el-input v-model="catalogueForm.description" :autosize="{ minRows: 2, maxRows: 4}" type="textarea" placeholder="请输入产品介绍" />
      </el-form-item>
      <el-form-item label="备注">
        <el-input v-model="catalogueForm.remark" :autosize="{ minRows: 2, maxRows: 4}" type="textarea" placeholder="Please input" />
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="handleCannel()">
        取消
      </el-button>
      <el-button type="primary" @click="createData()">
        保存并发布
      </el-button>
      <el-button v-if="catalogueForm.status.value <= 5" type="primary" @click="draftData()">
        保存
      </el-button>
      <el-button v-if="catalogueForm.status.value === 5 && catalogueForm.ID > 0" type="danger" @click="handleDelete(catalogueForm)">
        删除
      </el-button>
    </div>
  </div>
</template>

<script>
import { saveCatalogue, removeCatalogueByID, getCatalogue } from '@/api/supply/catalogue'
import waves from '@/directive/waves' // waves directive
import Upload from '@/components/Upload/SingleImage' // secondary package based on el-pagination
import { } from '@/utils'

export default {
  name: 'ComplexTable',
  components: { Upload },
  directives: { waves },
  filters: {
  },
  data() {
    return {
      tableKey: 0,
      catalogueForm: {
        title: '',
        description: '',
        catalogue_identity: 0,
        remark: '',
        status: { label: '', value: 1 }
      },
      rules: []
    }
  },
  created() {
    let catalogueID = this.$route.query && this.$route.query.catalogueID
    if (catalogueID !== undefined) {
      catalogueID = parseInt(catalogueID)
      if (!isNaN(catalogueID)) {
        this.listLoading = true
        const formData = {
          'catalogueID': catalogueID
        }
        getCatalogue(formData).then(response => {
          if (response.status === 200) {
            this.catalogueForm = response.data
          } else {
            this.$message({
              message: response.msg,
              type: 'error'
            })
          }
        })
      }
    }
    console.log(catalogueID)
  },
  methods: {
    handleCannel() {
      this.$router.push({ path: 'catalogue' })
    },
    draftData() {
      this.listLoading = true
      const _catalogueForm = this.catalogueForm
      _catalogueForm.status = 1
      if (_catalogueForm.ID > 0) {
        _catalogueForm.catalogueID = _catalogueForm.ID
      }
      saveCatalogue(_catalogueForm).then(response => {
        if (response.status === 200) {
          this.$message({
            message: '操作Success',
            type: 'success'
          })
          this.$router.push({ path: 'catalogue' })
        } else {
          this.$message({
            message: response.msg,
            type: 'error'
          })
        }
      })
    },
    createData() {
      this.listLoading = true
      const _catalogueForm = this.catalogueForm
      _catalogueForm.status = 6
      if (_catalogueForm.ID > 0) {
        _catalogueForm.catalogueID = _catalogueForm.ID
      }
      saveCatalogue(_catalogueForm).then(response => {
        if (response.status === 200) {
          this.$message({
            message: '操作Success',
            type: 'success'
          })
          this.$router.push({ path: 'catalogue' })
        } else {
          this.$message({
            message: response.msg,
            type: 'error'
          })
        }
      })
    },
    handleDelete(row, index) {
      const _this = this
      const formData = {
        'catalogueID': row.ID
      }
      console.log(index)
      removeCatalogueByID(formData).then(response => {
        if (response.status === 200) {
          this.$message({
            message: '操作Success',
            type: 'success'
          })
          _this.$router.push({ path: 'catalogue' })
        } else {
          this.$message({
            message: response.msg,
            type: 'error'
          })
        }
      })
    }
  }
}
</script>
